<head>
    
</head>
<script>
    //object构造函数
    //缺点，语句太多
    var o = new Object();
    o = {};
    o.name = "小明";
    o.age = 18;
    o.setName = function(name){
        this.name = name;
    }

    //缺点，创建单个对象时可使用
    var o2 ={    
        name:"小红",//标识符作用域链
        age: 20,
        setName: function(name){//函数对象（属性）原型链
            this.name = name;
        }
    }


    //返回一个对象的函数是工厂模式
    //缺点，每创建的对象都是object类型
    function CreateObj(name ,age){
        var o3 = {
            name:name,
            age:age,
            setName:function(name){
                this.name = name;
            }
        }
        return o3;
    }

    //自定义构造函数
    //缺点，每次重复创建对象，setName在prototype中，指向同一个，浪费内存
    function Person(name,age){
        this.name = name;
        this.age = age;
        this.setName = function(name){
            this.name = name;
        }
    }
    var p1 = new Person("小明",18);
    var p2 = new Person("lxy",18);

    //自定义构造函数+原型组合
    //优点，创建对象时，setName在prototype中，指向同一个，节省内存
    function Person2(name,age){
        this.name = name;
        this.age = age;
    }

    Person2.prototype.setName = function(name){
        this.name = name;
    }
</script>